package com.me.service;


import com.me.mapper.PokemonMapper;
import com.me.pojo.Pokemon;
import com.me.util.SqlSessionFactoryUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class PokemonService {

    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    /**
     * 查询所有
     *
     * @return
     */
    public List<Pokemon> selectall() {
        //调用BrandMapper.selectall()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        PokemonMapper mapper = sqlSession.getMapper(PokemonMapper.class);

        //4. 调用方法
        List<Pokemon> pokemons = mapper.selectall();

        sqlSession.close();

        return pokemons;
    }

    /**
     * 添加
     *
     * @param pokemon
     * @return
     */
    public void addpokemon(Pokemon pokemon) {

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        PokemonMapper mapper = sqlSession.getMapper(PokemonMapper.class);

        //4. 调用方法
        mapper.addpokemon(pokemon);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }

    public Pokemon selectpokemonbyid(int id) {
        //调用PokemonMapper.selectbyid()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        PokemonMapper mapper = sqlSession.getMapper(PokemonMapper.class);

        //4. 调用方法
        Pokemon pokemons = mapper.selectpokemonbyid(id);


        sqlSession.close();

        return pokemons;
    }

    public void updatepokemon(Pokemon pokemon) {
        //调用PokemonMapper.pdatepokemon()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3. 获取BrandMapper
        PokemonMapper mapper = sqlSession.getMapper(PokemonMapper.class);

        //4. 调用方法
        mapper.updatepokemon(pokemon);

        //提交事务
        sqlSession.commit();

        //5.释放资源

        sqlSession.close();

    }
    public void deletepokemon(int id) {
        //调用PokemonMapper.selectbyid()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        PokemonMapper mapper = sqlSession.getMapper(PokemonMapper.class);

        //4. 调用方法
        mapper.deletepokemon(id);

        //提交事务
        sqlSession.commit();

        //5.释放资源

        sqlSession.close();

    }
    //高级模糊搜索
    public List<Pokemon> searchpokemonBykeyword(String keyword) {
        //调用BrandMapper.selectall()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        PokemonMapper mapper = sqlSession.getMapper(PokemonMapper.class);

        //4. 调用方法
        List<Pokemon> pokemons = mapper.searchpokemonBykeyword(keyword);

        sqlSession.close();

        return pokemons;
    }
    public Integer pokemonCount() {
        //调用PokemonMapper.pokemonCount()
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        PokemonMapper mapper = sqlSession.getMapper(PokemonMapper.class);

        //4. 调用方法
        Integer count = mapper.pokemonCount();

        //5.释放资源
        sqlSession.close();

        return count;
    }

}
